using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FrameWork.TileMapManager;

public class PlayerAStar : MonoBehaviour
{
    [SerializeField] private float playerAStarSpeed = 4f;
    [SerializeField] private Animator animator;
    private Queue<Vector3> path;
    private bool isMoving;
    
    [Header("位置移动偏移")]
    [SerializeField] private float offsetX = 0.5f;
    [SerializeField] private float offsetY = 0.5f;
    
    // 设置朝向
    private float horizontal;
    private float vertical;

    private void Start()
    {
        animator = GetComponent<Animator>();
        
        // 变量初始化
        vertical = 1f;
        horizontal = 1f;
    }

    private void Update()
    {
        if (PlayerInput.Instance.GetIsUseTool && !isMoving && PlayerInput.Instance.MouseState == MouseState.AStar)
        {
            animator.Play("PlayerWalk");
            path = AStarAndGetTilesData.Instance.AStarFindPath(transform.position, PlayerInput.Instance.MouseWorldPosition);
            if (path != null && path.Count > 1) // 确保队列中至少有两个元素
            {
                // 丢弃第一个元素，这个元素是玩家当前所处的位置
                path.Dequeue();
                StartCoroutine(MoveAlongPath());
            }
        }

        // 如果不再移动中，播放空闲动画
        if (!isMoving && PlayerInput.Instance.MouseState == MouseState.AStar)
        {
            animator.Play("PlayerIdle");
        }

        SetFaceDir();
    }

    private IEnumerator MoveAlongPath()
    {
        isMoving = true;
        while (path.Count > 0)
        {
            // 取出元素
            Vector3 target = path.Dequeue();
            // 设置偏移位置
            Vector3 targetPosition = new Vector3(target.x + offsetX, target.y + offsetY, target.z);
            while (Vector3.Distance(transform.position, targetPosition) > 0.1f)
            {
                transform.position = Vector3.MoveTowards(transform.position, targetPosition,
                    playerAStarSpeed * Time.deltaTime);

                if (targetPosition.x > transform.position.x)
                {
                    horizontal = 1f;
                }
                else if (targetPosition.x < transform.position.x)
                {
                    horizontal = -1f;
                }

                if (targetPosition.y > transform.position.y)
                {
                    vertical = 0.5f;
                }
                else if (targetPosition.y < transform.position.y)
                {
                    vertical = 1f;
                }
                else
                {
                    vertical = 0f;
                }
                yield return null;
            }
        }
        isMoving = false;
    }

    /// <summary>
    /// 设置玩家朝向
    /// </summary>
    private void SetFaceDir()
    {
        // 不再AStar寻路，使用由按键驱动玩家朝向
        if (PlayerInput.Instance.MouseState == MouseState.AStar)
        {
            // 设置 动画机朝向 以及 玩家Scale朝向
            animator.SetFloat("Vertical", vertical);
            transform.localScale = new Vector3(horizontal, 1, 1);
        }
        else
        {
            // 设置 动画机朝向 以及 玩家Scale朝向
            animator.SetFloat("Vertical", PlayerInput.Instance.Vertical);
            transform.localScale = new Vector3(PlayerInput.Instance.Horizontal, 1, 1);
        }
    }
}
